package com.delivery.utils;

import lombok.SneakyThrows;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;


@Component
public class RedisKeyExpirationListenerUtil extends KeyExpirationEventMessageListener {


        public RedisKeyExpirationListenerUtil(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }

        /**
         * 使用该方法监听，当我们key过期是自动调用该方法
         * @param message
         * @param pattern
         */
        @SneakyThrows
        @Override
        public void onMessage(Message message, byte[] pattern) {
            String s = message.toString();//就是过期的key
            String[] split = s.split(":");

            //寄件订单过期
            if (split[0].equals("delivery-order")){
                String code = split[1];
            //关闭腾讯订单
            WxMiniPayUtils.closeOrder(code);
            }

            //补缴订单过期
            if (split[0].equals("delivery-order-compensation")){
                String code = split[1];
                //关闭腾讯订单
                WxMiniPayUtils.closeOrder(code);
            }

        }


}
